package cn.com.blueInfo.HaiYuan.business.drugs.service.impl;

import cn.com.blueInfo.HaiYuan.business.drugs.entity.DrugsBase;
import cn.com.blueInfo.HaiYuan.business.drugs.mapper.DrugsBaseMapper;
import cn.com.blueInfo.HaiYuan.business.drugs.service.DrugsBaseService;
import cn.com.blueInfo.utils.result.BootstrapTable;
import cn.com.blueInfo.utils.result.ResultInfo;
import cn.com.blueInfo.utils.util.ChangeToPinYinJP;
import cn.com.blueInfo.utils.util.JSONUtil;
import cn.com.blueInfo.utils.util.RequestPayload;
import cn.com.blueInfo.utils.util.UUIDUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 库存基本信息服务接口具体实现
 * @ClassName: DrugsBaseServiceImpl
 * @author suxch
 * @date 2019年4月24日  上午9:34:39
 */
@Log4j2
@Service
public class DrugsBaseServiceImpl implements DrugsBaseService {

    @Autowired
    private DrugsBaseMapper baseMapper;

    /**
     * 分页查询药品基本数据具体实现
     * @Title: queryDrugsBaseDataByPage
     * @param request
     * @return BootstrapTable
     */
    @Override
    public BootstrapTable queryDrugsBaseDataByPage(HttpServletRequest request) {
        BootstrapTable result = new BootstrapTable();
        JSONObject param = RequestPayload.analysis(request);
        log.info("bootstrapTable请求参数" + param);

        Map<String, Object> paramMap = JSON.parseObject(param.toJSONString());

        try {
            Integer count = baseMapper.queryDrugsBaseInfoCount(paramMap);
            List<DrugsBase> drugsBaseList = baseMapper.queryDrugsBaseInfo(paramMap);
            result.setTotal(count);
            result.setRows(drugsBaseList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result.setTotal(0);
            result.setRows(null);
        }
        return result;
    }

    /**
     * 查询药品基本数据具体实现
     * @Title: queryDrugsBaseData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo queryDrugsBaseData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();

        String requestData = request.getParameter("requestParam");
        Map<String, Object> paramMap = JSON.parseObject(requestData);

        try {
            List<DrugsBase> drugsBaseList = baseMapper.queryDrugsBaseInfo(paramMap);
            result.setResult(true);
            result.setMessage("药品基本信息获取成功");
            result.setData(drugsBaseList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result.setResult(false);
            result.setMessage("药品基本信息获取成功，请联系管理员");
            result.setError(e.getMessage());
        }
        return result;
    }

    /**
     * 通过药品名称查询拼音缩写具体实现
     * @Title: queryRenPingByDrugName
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo queryRenPingByDrugName(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        ChangeToPinYinJP pinyin = new ChangeToPinYinJP();
        String drugName = request.getParameter("drugName");
        try {
            String pinyinStr = pinyin.changeToGetShortPinYin(drugName);
            result.setResult(true);
            result.setMessage("拼音缩写获取成功");
            result.setData(pinyinStr);
        } catch (Exception e) {
            result.setResult(false);
            result.setMessage("拼音缩写获取失败，请联系管理员");
            result.setError(e.getMessage());
            log.error(e.getMessage());
        }
        return result;
    }

    /**
     * 查询最大排序编号具体实现
     * @Title: queryMaxOrderNumber
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo queryMaxOrderNumber(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        try {
            Integer maxNum = baseMapper.queryMaxOrderNumber();
            if (maxNum == null) maxNum = 0;
            maxNum += 1;
            result.setData(maxNum);
        } catch (Exception e) {
            result.setResult(false);
            result.setMessage("最大编号获取失败，请联系管理员");
            result.setError(e.getMessage());
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 新增药品基本数据具体实现
     * @Title: addDrugsBaseData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo addDrugsBaseData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String requestData = request.getParameter("requestParam");

        if (JSONUtil.isToJSONObject(requestData)) {
            DrugsBase drugsBase = JSON.parseObject(requestData, DrugsBase.class);
            drugsBase.setUnid(UUIDUtil.createUUID());

            Map<String, Object> queryParam = new HashMap<String, Object>();
            queryParam.put("commodityName", drugsBase.getCommodityName());
            queryParam.put("currencyName", drugsBase.getCurrencyName());
            queryParam.put("domainCode", drugsBase.getDomainCode());

            try {
                Integer isExist = baseMapper.queryDrugsBaseInfoCount(queryParam);
                if (isExist == 1) {
                    result.setResult(false);
                    result.setMessage("该药品基本信息已存在，请修改");
                } else {
                    int add = baseMapper.addDrugsBaseInfo(drugsBase);
                    if (add == 1) {
                        result.setResult(true);
                        result.setMessage("药品基本信息添加成功");
                    }
                }
            } catch (Exception e) {
                result.setResult(false);
                result.setError(e.getMessage());
                result.setMessage("药品基本信息增加失败，请联系管理员");
                log.error(e.getMessage());
                e.printStackTrace();
            }
        } else {
            result.setResult(false);
            result.setMessage("参数异常，请联系管理员");
        }
        return result;
    }

    /**
     * 更新药品基本数据具体实现
     * @Title: updateDrugsBaseData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo updateDrugsBaseData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String requestData = request.getParameter("requestParam");

        if (JSONUtil.isToJSONObject(requestData)) {
            DrugsBase drugsBase = JSON.parseObject(requestData, DrugsBase.class);

            try {
                int update = baseMapper.updateDrugsBaseInfoByUnid(drugsBase);
                if (update == 1) {
                    result.setResult(true);
                    result.setMessage("药品基本信息更新成功");
                }
            } catch (Exception e) {
                result.setResult(false);
                result.setMessage("药品基本信息更新失败，请联系管理员");
                result.setError(e.getMessage());
                log.error(e.getMessage());
                e.printStackTrace();
            }
        } else {
            result.setResult(false);
            result.setMessage("参数异常");
        }
        return result;
    }

    /**
     * 删除药品基本数据具体实现
     * @Title: deleteDrugsBaseData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo deleteDrugsBaseData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String drugsUnid = request.getParameter("unid");

        try {
            int update = baseMapper.delDrugsBaseInfoByUnid(drugsUnid);
            if (update == 1) {
                result.setResult(true);
                result.setMessage("药品基本信息删除成功");
            }
        } catch (Exception e) {
            result.setResult(false);
            result.setMessage("药品基本信息删除失败，请联系管理员");
            result.setError(e.getMessage());
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

}
